<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\common\library\Sms;
use fast\Random;
use think\Validate;
use think\Exception;
use think\Hook;
use app\common\library\Token;
use app\admin\controller\finance\Alipay;


/**
 * 个人中心
 */
class Personal extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];



    /**
     * @ApiTitle    (个人中心首页)
     * @ApiSummary  (个人中心首页)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/buyList)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
        "time": "2021-10-26",
        "user": 用户信息,
        "noPay": 待支付,
        "onPay": 待发付,
        "inPay": 交易中,
        "onGoods": 待发货,
        "inGoods": 已发货,
        "okBuy": 买家成交数,
        "okSell": 卖家成交数,
        "goods": 上架中
        "okb": 买家成交率
        "oks": 卖家成交率

     * })
     */
    public function buyList()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $user = Db::name('personal')->field('id,account,nickname,balance,jy_balance,is_real')->where('id',$id)->find();
        $time = date('Y-m-d',time());
        $noPay = Db::name('order')->where('buyid',$user['id'])->where('status',1)->count();
        $onPay = Db::name('order')->where('buyid',$user['id'])->where('status',2)->count();
        $inPay = Db::name('order')->where('buyid',$user['id'])->where('status',3)->count();
        $okBuy = Db::name('order')->where('buyid',$user['id'])->where('status',5)->count();
        $okb = Db::name('order')->where('buyid',$user['id'])->count();
        $onGoods = Db::name('order')->where('uid',$user['id'])->where('status',2)->count();
        $inGoods = Db::name('order')->where('uid',$user['id'])->where('status',3)->count();
        $okSell = Db::name('order')->where('uid',$user['id'])->where('status',5)->count();
        $oks = Db::name('order')->where('uid',$user['id'])->count();
        $goods = Db::name('goods')->where('uid',$user['id'])->where('status',1)->count();
        $recommend = Db::name('goods')
            ->field('id,gamename,type_name,title,price')
            ->where('status',1)
            ->where('goods_class_type','neq',1)
            ->order('createtime desc')
            ->limit(1)
            ->select();
        $res['time'] = $time;
        $res['user'] = $user;
        $res['noPay'] = $noPay;
        $res['onPay'] = $onPay;
        $res['inPay'] = $inPay;
        $res['onGoods'] = $onGoods;
        $res['inGoods'] = $inGoods;
        $res['okBuy'] = $okBuy;
        $res['okSell'] = $okSell;
        $res['goods'] = $goods;
        $res['recommend'] = $recommend;
        $res['okb'] = @intval(($okBuy/$okb) * 100) . '%';
        $res['oks'] = @intval(($okSell/$oks) * 100) . '%';
        $this->success('ok',$res);
    }


    public function online()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $type = $this->request->post('type');
        if ($type == 0) {   // 开启
            Db::name('goods')->where('uid',$id)->update(['status'=>0]);
            Db::name('personal')->where('id',$id)->update(['online'=>0]);
        } elseif ($type == 1) {     // 关闭
            Db::name('goods')->where('uid',$id)->update(['status'=>1]);
            Db::name('personal')->where('id',$id)->update(['online'=>1]);
        }
        $this->success('ok');
    }

    /**
     * @ApiTitle    (我购买的商品)
     * @ApiSummary  (我购买的商品)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/imBuy)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams	(name="keyword", type=varchar, required=false, description="关键字")
     * @ApiParams   (name="type", type="varchar", required=false, description="1:待支付 2:待发货 3:已发货 4:交易中没用 5:已完成 6:已赔付 7:撤单 8:待审核 9:取消订单 11:待验证 12:待确认")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
            "id": 7,
            "order_sn": 订单号,
            "type_name": 游戏类型名称,
            "game": 游戏名,
            "area": 区服,
            "title": 标题,
            "price": 价格,
            "num": 数量,
            "amount": "总价",
            "mobile": "15611422222",
            "ontime": 下单时间,
            "paytime": 支付时间,
            "overtime": 完成订单时间,
            "status":  1:待支付 2:待发货 3:已发货 4:交易中 5:已完成 6:已赔付 7:取消
            "reason":  原因
    }
     * })
     */
    public function imBuy()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $type = $this->request->post('type');
        $keyword = $this->request->post('keyword');
        if (!empty($keyword)) {
            $map['title'] = array('like','%'.$keyword.'%');
        }
        $map['buyid'] = $id;
        if (!empty($type)) {
            $map['status'] = $type;
            $res = Db::name('order')
                ->field('id,order_sn,goods_id,game_id,image,price,type_name,buy_type,title,game,area,server,num,amount,ontime,status,reason,complaint,admin_id,uid')
                ->where($map)
                ->order('ontime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $index = new Index();
                    $admin = Db::name('admin')->where('id',$item['admin_id'])->find();
                    $user = Db::name('personal')->where('id',$item['uid'])->find();
                    //卖家昵称
                    $item['othername'] = $user['nickname'];
                    $item['userToken'] = $index->getJt($user['id'],$user['account'],$user['avatar']);
                    $item['adminToken'] = $index->getJt('admin'.$admin['id'],$admin['username'],$admin['avatar']);
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        } else {
            $res = Db::name('order')
                ->field('id,order_sn,goods_id,game_id,image,price,type_name,buy_type,title,game,area,server,num,amount,ontime,status,reason,complaint,admin_id,uid')
                ->where($map)
                ->order('ontime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $index = new Index();
                    $admin = Db::name('admin')->where('id',$item['admin_id'])->find();
                    $user = Db::name('personal')->where('id',$item['uid'])->find();
                    $item['userToken'] = $index->getJt($user['id'],$user['account'],$user['avatar']);
                    $item['adminToken'] = $index->getJt('admin'.$admin['id'],$admin['username'],$admin['avatar']);
                    $item['image'] = config('api_image_url') . $item['image'];
                    //卖家昵称
                    $item['othername'] = $user['nickname'];
                    return $item;
                });
        }
        $this->success('ok',$res);
    }

    /**
     * @ApiTitle    (我卖出的订单)
     * @ApiSummary  (我卖出的订单)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/imSell)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams	(name="keyword", type=varchar, required=false, description="关键字")
     * @ApiParams   (name="type", type="varchar", required=false, description="1:待支付 2:待发货 3:已发货 4:交易中 5:已完成 6:已赔付 7:取消")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
        "code": 1,
        "msg": "ok",
        "time": "1627553755",
        "data": {
            "id": 7,
            "order_sn": 订单号,
            "type_name": 游戏类型名称,
            "game": 游戏名,
            "area": 区服,
            "title": 标题,
            "price": 价格,
            "num": 数量,
            "amount": "总价",
            "mobile": "15611422222",
            "ontime": 下单时间,
            "paytime": 支付时间,
            "overtime": 完成订单时间,
            "status":  1:待支付 2:待发货 3:已发货 4:交易中 5:已完成 6:已赔付 7:取消
            "reason":  原因
            "complaint":  是否投诉(0:未投诉 1:投诉)

        }
     * })
     */
    public function imSell()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $type = $this->request->post('type');
        $keyword = $this->request->post('keyword');
        if (!empty($keyword)) {
            $map['title'] = array('like','%'.$keyword.'%');
        }
        $map['uid'] = $id;
        if (!empty($type)) {
            $map['status'] = $type;
            $res = Db::name('order')
                ->field('id,order_sn,goods_id,image,price,type_name,title,game,area,server,num,amount,ontime,status,reason,complaint,admin_id,buyid,buy_type')
                ->where($map)
                ->order('ontime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $index = new Index();
                    $admin = Db::name('admin')->where('id',$item['admin_id'])->find();
                    $user = Db::name('personal')->where('id',$item['buyid'])->find();
                    $item['userToken'] = $index->getJt($user['id'],$user['account'],$user['avatar']);
                    $item['adminToken'] = $index->getJt('admin'.$admin['id'],$admin['username'],$admin['avatar']);
                    //买家昵称
                    $item['othername'] = $user['nickname'];
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        } else {
            $res = Db::name('order')
                ->field('id,order_sn,goods_id,image,price,type_name,title,game,area,server,num,amount,ontime,status,reason,complaint,admin_id,buyid,buy_type')
                ->where($map)
                ->order('ontime desc')
                ->paginate(10,false,['query'=>request()->param()])
                ->each(function ($item,$key){
                    $index = new Index();
                    $admin = Db::name('admin')->where('id',$item['admin_id'])->find();
                    $user = Db::name('personal')->where('id',$item['buyid'])->find();
                    $item['userToken'] = $index->getJt($user['id'],$user['account'],$user['avatar']);
                    $item['adminToken'] = $index->getJt('admin'.$admin['id'],$admin['username'],$admin['avatar']);
                    //买家昵称
                    $item['othername'] = $user['nickname'];
                    $item['image'] = config('api_image_url') . $item['image'];
                    return $item;
                });
        }
        $this->success('ok',$res);
    }

    /**
     * @ApiTitle    (我的商品)
     * @ApiSummary  (我的商品)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/sellGoods)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="varchar", required=false, description="1:上架 2:用户下架 3:审核失败 4:审核中 5:平台下架")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
            "id": 3,
            "goods_sn": 编号,
            "type_name": 类型名
            "createtime": 创建时间
            "price": 价格
            "image": 图片
            "title": 标题
            "gamename": 游戏名
            "area": 区服
            "stock": 库存
            "status": 1:上架 2:用户下架 3:审核失败 4:审核中 5:平台下架
            "auth_status": 0未提交1审核中2已认证3审核失败
            "havetime": 还有多少天
            "reason":原因
        }
     * })
     */
    public function sellGoods()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $type = $this->request->post('type');
        $data = Db::name('goods')
            ->field('id,goods_sn,type_name,createtime,prices as price,image,gameid,title,gamename,area,server,stock,status,havetime,reason,auth_status,is_safe')
            ->where('uid',$id)
            ->where('stock','gt',0)
            ->where('status',$type)
            ->order('createtime desc')
            ->paginate(10,false,['query'=>request()->param()])
            ->each(function ($item,$key){
                $item['havetime'] = intval(($item['havetime']-$item['createtime'])/86400);
                $item['image'] = config('api_image_url') . $item['image'];
                return $item;
            });
        $this->success('ok',$data);
    }


    /**
     * @ApiTitle    (商品下架)
     * @ApiSummary  (商品下架)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/undercarriage)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=false, description="下架商品的id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "下架成功",
    "time": "1627553755",

     * })
     */
    public function undercarriage()
    {
        $token = $this->request->header('token');
        $this->is_login($token);
        $id = $this->request->post('id');
        $ids = explode(',',$id);
        foreach ($ids as $k=>$v) {
            Db::name('goods')->where('id',$v)->update(['status'=>2]);
        }
        $this->success('下架成功');
    }


    /**
     * @ApiTitle    (商品上架)
     * @ApiSummary  (商品上架)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/carriage)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=false, description="下架商品的id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "下架成功",
    "time": "1627553755",

     * })
     */
    public function carriage()
    {
        $id = $this->request->post('id');
        $token = $this->request->header('token');
        $this->is_login($token);
        $ids = explode(',',$id);
        foreach ($ids as $k=>$v) {
            $goods = Db::name('goods')->where('id',$v)->find();
            $time = ($goods['havetime'] - $goods['createtime']) / 86400;
            Db::name('goods')->where('id',$v)->update(['status'=>1,'createtime'=>time(),'havetime'=>time()+86400*$time]);
        }
        $this->success('上架成功');
    }


    /**
     * @ApiTitle    (商品删除)
     * @ApiSummary  (商品删除)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/goodsDel)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=false, description="下架商品的id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "下架成功",
    "time": "1627553755",

     * })
     */
    public function goodsDel()
    {
        $token = $this->request->header('token');
        $this->is_login($token);
        $id = $this->request->post('id');
        $ids = explode(',',$id);
        foreach ($ids as $k=>$v) {
            Db::name('goods')->where('id',$v)->update(['status'=>9]);
        }
        $this->success('下架成功');
    }


    /**
     * @ApiTitle    (资金管理)
     * @ApiSummary  (资金管理)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/capital)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="varchar", required=false, description="1:购买 2:出售 3:充值 4:提现")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
        "balance": {
            "id": 3,
            "balance": 余额,
            "jy_balance": 赔付金
            "consume": 消费金额
            "buy": 售得
            "withdrawal": 提现
            "payfor": 赔付
        },
        "detailed": [
            "id": 3,
            "log_sn": 单号,
            "createtime": 时间
            "money": 金额
            "type": 类型 1:购买 2:出售 3:充值 4:提现
            "status": 1:交易完成 2:未完成
            "content": 备注
          ]
    }
     * })
     */
    public function capital()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $type = input('type');
        $balance = Db::name('personal')->field('id,balance,jy_balance')->where('id',$id)->find();
        $balance['consume'] = Db::name('order')
            ->where('buyid',$id)
            ->where('status','neq',0)
            ->where('status','neq',1)
            ->where('status','neq',6)
            ->where('status','neq',7)
            ->where('status','neq',8)
            ->where('status','neq',9)
            ->where('status','neq',10)
            ->where('status','neq',11)
            ->where('status','neq',12)
            ->sum('amount');
        $balance['buy'] = Db::name('account_log')
            ->where('type',2)
            ->where('uid',$id)
            ->where('status',1)
            ->sum('money');
        $balance['withdrawal'] = Db::name('withdrawal')->where('uid',$id)->where('status',1)->sum('money');
        $balance['payfor'] = Db::name('order')->where('uid',$id)->where('status',6)->sum('amount');
        if (empty($type)) {
            $detailed = Db::name('account_log')->where('uid',$id)->order('createtime desc')->paginate(10);
        } elseif ($type == 5) {
            $detailed = Db::name('account_log')->where('form',2)->where('uid',$id)->order('createtime desc')->paginate(10);
        } else {
            $detailed = Db::name('account_log')->where('type',$type)->where('uid',$id)->order('createtime desc')->paginate(10);
        }


        $data['balance'] = $balance;
        $data['detailed'] = $detailed;
        $this->success('ok',$data);
    }




    /**
     * @ApiTitle    (新增提现账户页面)
     * @ApiSummary  (新增提现账户页面)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/cashList)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
            "name": 名字
            "mobile": 手机
            "bank": {  收费标准
                "id":
                "name": 名称
            },
        },
     * })
     */
    public function cashList()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $res = Db::name('personal')->field('name,mobile')->where('id',$id)->find();
        $res['name'] = '*' . mb_substr($res['name'],1,3,"utf-8");
        $res['bank'] = Db::name('bank')->where('is_bank',1)->where('status','normal')->select();
        $this->success('ok',$res);
    }




    /**
     * @ApiTitle    (新增提现账户)
     * @ApiSummary  (新增提现账户)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/cash)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="mobile", type="varchar", required=false, description="电话")
     * @ApiParams   (name="captcha", type="varchar", required=false, description="验证码")
     * @ApiParams   (name="type", type="varchar", required=false, description="1银行卡,2支付宝")
     * @ApiParams   (name="card", type="varchar", required=false, description="银行卡号/支付宝账号")
     * @ApiParams   (name="cards", type="varchar", required=false, description="重复 银行卡号/支付宝账号")
     * @ApiParams   (name="bank", type="varchar", required=false, description="开户银行名称/支付宝")
     * @ApiParams   (name="bank_id", type="varchar", required=false, description="开户银行名称 id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "新增提现账户成功",
    "time": "1627553755",

     * })
     */
    public function cash()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $mobile = $this->request->post('mobile');
        $captcha = $this->request->post('captcha');
        $type = $this->request->post('type');
        $card = $this->request->post('card');
        $cards = $this->request->post('cards');
        $bank = $this->request->post('bank');
        $bank_id = $this->request->post('bank_id');
        $bank = $bank ? $bank : '支付宝';
        $bank_id = $bank_id ? $bank_id : 8;
        $cat = Db::name('cash_account')->where('card',$card)->where('is_del',0)->find();
        if (!empty($cat)) {
	        $this->error(__('请不要重复'));
        }
        if (!Sms::check($mobile, $captcha, 'withdrawal')) {
            $this->error(__('请填写正确的验证码'));
        }
        if ($card != $cards) {
            $this->error(__('请确认账号是否正确'));
        }
        if (empty($bank) || empty($card)) {
            $this->error(__('请完善信息'));
        }
        $name = Db::name('personal')->where('id',$id)->value('name');
        $res = [
            'uid'   =>  $id,
            'type'  =>  $type,
            'name'  =>  $name,
            'bank'  =>  $bank,
            'bank_id'  =>  $bank_id,
            'card'   =>  $card,
            'default'   =>  0,
            'is_del'    =>  0,
            'createtime'    =>  time()
        ];
        $data = Db::name('cash_account')->insert($res);
        if ($data) {
            $this->success('新增提现账户成功');
        } else {
            $this->error('新增提现账户失败');
        }
    }


    /**
     * @ApiTitle    (提现页面)
     * @ApiSummary  (提现页面)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/withdrawalList)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
        "name": 名字
        "balance": 可提现余额
        "fees": {  收费标准
            "id":id
            "name": 名称
            "is_bank":  1:银行卡 2:支付宝
            "sc_radio": 手续费比例（%）
            "min_fee": 最低手续费（元/笔）
            "max_fee": 最高手续费（元/笔）
        },
       "account": {  提现信息
            "id":
            "name": 姓名
            "bank": 提现类型
            "card": 卡号
        },
    },
     * })
     */
    public function withdrawalList()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $user = Db::name('personal')->where('id',$id)->find();
        if ($user['is_real'] != 1) {
            $this->error('未实名',[],101);
        }
        $res['name'] = '*' . mb_substr($user['name'],1,3,"utf-8");
        $res['balance'] = $user['balance'];
        $res['fees'] = Db::name('bank')->where('status','normal')->select();
        $res['account'] = Db::name('cash_account')->field('id,name,bank,card')->where('uid',$id)->select();
        $this->success('ok',$res);
    }


    /**
     * @ApiTitle    (提现)
     * @ApiSummary  (提现)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/withdrawal)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="cid", type="varchar", required=false, description="体现账户的id")
     * @ApiParams   (name="balance", type="varchar", required=false, description="提现金额")
     * @ApiParams   (name="pass", type="varchar", required=false, description="支付密码")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
        "code": 1,
        "msg": "提现成功,请稍等",
        "time": "1627553755",

     * })
     */
    public function withdrawal()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $cid = $this->request->post('cid');
        $balance = $this->request->post('balance');
        $pass = $this->request->post('pass');
        $pay_pass = Db::name('personal')->where('id',$id)->find();
        if (empty($pay_pass['paypass'])) {
            $this->error('请设置支付密码','',110);
        }
        if (md5($pass) != $pay_pass['paypass']) {
            $this->error(__('支付密码不正确，请重试'));
        }
	    //添加分组，只有投诉咨询和一级客服可以分配
	    $auth_group = DB::name("auth_group")->where(['name'=>['in',['投诉咨询','一级客服']]])->column('id');
	    $auth_group_access = DB::name("auth_group_access")->where(['group_id'=>['in',$auth_group]])->column("uid");
	    $admin_id = Db::name('admin')->where(['status'=>'normal','id'=>['in',$auth_group_access]])->orderRaw('Rand()')->value('id');
        if ($balance < 3) {
            $this->error(__('提现金额不小于￥3'));
        }
        if ($balance > 50000) {
            $this->error(__('提现金额不大于￥50000'));
        }
        $u = Db::name('cash_account')->where('id',$cid)->find();
        $user = Db::name('personal')->where('id',$id)->find();
        if ($user['balance'] - $balance < 0) {
            $this->error(__('您可提现金额不足'));
        }
        $fee = Db::name('bank')->where('id',$u['bank_id'])->find();
        $fees = $balance * $fee['sc_radio']*0.01;
        if ($fees < $fee['min_fee']) {
            $fees = $fee['min_fee'];
        }
        if ($fees > $fee['max_fee']) {
            $fees = $fee['max_fee'];
        }
        $time = date('Y-m-d',time());
        $count = Db::name('withdrawal')->where('uid',$id)->where('FROM_UNIXTIME(createtime,"%Y-%m-%d")="'.$time.'"')->count();
        $res = [
            'log_sn'    =>  'W' . $this->getRefundSn($id),
            'createtime'    =>  time(),
            'uid'   =>  $id,
            'name'  =>  $u['name'],
            'bank'  =>  $u['bank'],
            'number'    =>  $u['card'],
            'money' =>  $balance,
            'fee'   =>  $fees,
            'balance'   =>  $balance - $fees,
            'status'    =>  0
        ];
        $data = Db::name('withdrawal')->insertGetId($res);
        if ($data) {
            Db::name('personal')->where('id',$id)->setDec('balance',$balance);
            $ret1 = [
                'uid'   =>  $id,
                'log_sn'    =>  date('YmdHis') . rand(10000000,99999999),
                'money' =>  '-'.$balance,
                'type'  =>  4,
                'content'   =>  '提现',
                'createtime'    =>  time(),
                'arrivaltime'   =>  time(),
                'status'    =>  1
            ];
            $ret2 = [
                'uid'   =>  $id,
                'log_sn'    =>  date('YmdHis') . rand(10000000,99999999),
                'money' =>  '-'.$fees,
                'type'  =>  4,
                'content'   =>  '手续费',
                'createtime'    =>  time(),
                'arrivaltime'   =>  time(),
                'status'    =>  1
            ];
            Db::name('account_log')->insert($ret1);
            Db::name('account_log')->insert($ret2);
            if ($u['bank'] == '支付宝' && $balance <= 5000 && $count < 3) {
                $row = Db::name('withdrawal')->where('id',$data)->find();
                $alipay = new Alipay();
                $tran_money = '0.1';
                $ret = $alipay->alipayTransfer($row['log_sn'], $row['number'],$row['name'], $tran_money, '提现');
                if($ret['code'] == 10000){
                    $params['order_id'] = $ret['order_id'];
                    $params['pay_fund_order_id'] = $ret['pay_fund_order_id'];
                    $params['out_biz_no'] = $ret['out_biz_no'];
                    $params['status'] = 1;
                    Db::name('withdrawal')->where('id',$data)->update($params);
                    $this->success('提现成功，请在支付宝内查收！');
                }else{
                    // 失败，处理业务逻辑
                    $this->error($ret['msg']);
                }
            }
            $easy = new Goeasy();
            $msg = '您有新的提现了，请审核！';
            $easy->sendToAdmin($admin_id,$msg);
            $this->success('提现成功，12小时内到账');
        } else {
            $this->error('提现失败');
        }
    }



    /**
     * @ApiTitle    (我的收藏)
     * @ApiSummary  (我的收藏)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/collection)
     * @ApiParams   (name="game", type="varchar", required=false, description="游戏id")
     * @ApiParams   (name="goods_type", type="varchar", required=false, description="类型id")
     * @ApiParams   (name="server", type="varchar", required=false, description="区服id")
     * @ApiParams   (name="keyword", type="varchar", required=false, description="关键字")
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": [
        {
            "id": 商品id,
            "title": 标题,
            "image": "http://qbl.780g.com",
            "gamename": 游戏名,
            "area": 区服,
            "status": 0:审核中 1:上架 2:下架 3:审核失败 4:客服下架,
            "price": 价格,
            "type_name": 类型名称
        }],

     * })
     */
    public function collection()
    {
        $token = $this->request->header('token');
        $game = $this->request->post('game');
        $goods_type = $this->request->post('goods_type');
        $server = $this->request->post('server');
        $keyword = $this->request->post('keyword');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $map['c.uid'] = $id;
        $map['c.status'] = 0;
        if (!empty($game)) {
            $map['g.gameid'] = $game;
        }
        if (!empty($goods_type)) {
            $map['g.goods_type'] = $goods_type;
        }
        if (!empty($server)) {
            $map['g.server_id'] = $server;
        }
        if (!empty($keyword)) {
            if (!Validate::is($keyword,'number')) {
                $map['g.title'] = array('like','%'.$keyword.'%');
            } else {
                $map['g.goods_sn'] = $keyword;
            }
        }
        $res = Db::name('collection')->alias('c')
            ->field('g.id,g.title,g.image,g.gamename,g.area,g.server,g.status,g.price,g.type_name,g.stock')
            ->join('goods g','g.id = c.gid')
            ->where($map)
            ->paginate(10,false,['query'=>request()->param()])
            ->each(function ($item,$key){
                if ($item['stock'] <= 0) {
                    $item['status'] = 9;
                }
                $item['image'] = config('api_image_url') . $item['image'];
                return $item;
            });
        $this->success('ok',$res);
    }


    /**
     * @ApiTitle    (实名认证)
     * @ApiSummary  (实名认证)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/realName)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="name", type="varchar", required=false, description="姓名")
     * @ApiParams   (name="captcha", type="varchar", required=false, description="验证码")
     * @ApiParams   (name="id_card", type="varchar", required=false, description="身份证号")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "恭喜!完成实名认证",
    "time": "1627553755",

     * })
     */
    public function realName()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $name = $this->request->post('name');
        $id_card = $this->request->post('id_card');
        $captcha = $this->request->post('captcha');
        $code = substr($id_card,6,4);
        if (date('Y',time()) - $code < 18) {
            $this->error(__('禁止未成年人使用本平台服务'));
        }
        $user = Db::name('personal')->where('id',$id)->find();
        $black = Db::name('personal')->where('id_card',$id_card)->where('switch',0)->select();
        if ($black) {
            $this->error(__('此身份证号已被拉入黑名单'));
        }
        if ($user['is_real'] == 1) {
            $real['name'] = '*' . mb_substr($user['name'],1,3,"utf-8");
            $real['id_card'] = substr($user['id_card'],0,1). '**************' . substr($user['id_card'],-1);
            $real['rztime'] = $user['rztime'];
            $this->success('ok',$real);
        }
        if (!Sms::check($user['mobile'], $captcha, 'realname')) {
            $this->error(__('请填写正确的验证码'));
        }
        if (!Validate::regex($id_card, "/^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/")) {
            $this->error(__('请填写正确的身份证'));
        }
//        if (!Validate::is($name, 'chsDian')) {
//            $this->error(__('请填写正确的姓名'));
//        }

        $realName = new Realname();
        $res = $realName->auth($id_card,$name);
        $result_arr = json_decode($res,true);
        if($result_arr['respCode'] == '0000'){
            $data = [
                'id_card'   =>  substr($id_card,0,1). '**************' . substr($id_card,-1),
                'name'  =>  '*' . mb_substr($name,1,3,"utf-8"),
                'rztime'    =>  time()
            ];
            Db::name('personal')->where('id',$id)->update(['id_card'=>$id_card,'name'=>$name,'rztime'=>time(),'is_real'=>1]);
            $this->success($result_arr['respMessage'],$data);
        }else{
            $this->error($result_arr['respMessage']);
        }
    }



    /**
     * @ApiTitle    (修改密码页面)
     * @ApiSummary  (修改密码页面)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/changePassList)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    'mobile':手机号
}
     * })
     */
    public function changePassList()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('mobile');
        $this->is_login($token);
        $this->success('ok',$id);
    }

    /**
     * @ApiTitle    (修改密码执行)
     * @ApiSummary  (修改密码执行)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/changePass)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="mobile", type="varchar", required=false, description="手机号")
     * @ApiParams   (name="captcha", type="varchar", required=false, description="验证码")
     * @ApiParams   (name="pass1", type="varchar", required=false, description="密码")
     * @ApiParams   (name="pass2", type="varchar", required=false, description="确认密码")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "修改密码成功",
    "time": "1627553755",


     * })
     */
    public function changePass()
    {
        $token = $this->request->header('token');
        $mobile = $this->request->post('mobile');
        $captcha = $this->request->post('captcha');
        $pass1 = $this->request->post('pass1');
        $pass2 = $this->request->post('pass2');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        if (!Sms::check($mobile, $captcha, 'changepass')) {
            $this->error(__('请填写正确的验证码'));
        }
        if (!Validate::is($pass1, '\S{8,20}')) {
            $this->error(__('密码不合格'));
        }
        if ($pass1 != $pass2) {
            $this->error(__('两次密码不一致'));
        }
        $salt = Random::alnum();
        $pass = md5(md5($pass1) .$salt);
        $data = Db::name('personal')->where('id',$id)->update(['pass'=>$pass,'salt'=>$salt]);
        if ($data) {
            $this->success('修改密码成功');
        } else {
            $this->error('修改密码失败');
        }
    }

    /**
     * @ApiTitle    (绑定手机号的手机)
     * @ApiSummary  (绑定手机号的手机)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/appleIdList)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    'mobile':手机号

     * })
     */
    public function appleIdList()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $mobile = Db::name('personal')->where('id',$id)->value('mobile');
        $this->success('ok',$mobile);
    }


    /**
     * @ApiTitle    (绑定手机)
     * @ApiSummary  (绑定手机)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/appleId)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="mobile", type="varchar", required=false, description="手机号")
     * @ApiParams   (name="captcha", type="varchar", required=false, description="验证码")
     * @ApiParams   (name="type", type="varchar", required=false, description="1 填验证码 2新绑定手机验证码")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "修改手机号成功",
    "time": "1627553755",


     * })
     */
    public function appleId()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $type = $this->request->post('type');
        $mobile = $this->request->post('mobile');
        $captcha = $this->request->post('captcha');
        if ($type == 1) {
            if (!Sms::check($mobile, $captcha, 'oldapple')) {
                $this->error(__('请填写正确的验证码'));
            } else {
                $this->success('验证成功');
            }
        } elseif ($type == 2) {
            if (!Sms::check($mobile, $captcha, 'apple')) {
                $this->error(__('请填写正确的验证码'));
            }
            $mobile = Db::name('personal')->where('mobile',$mobile)->find();
            if ($mobile) {
                $this->error(__('手机号已被绑定'));
            }
            $data = Db::name('personal')->where('id',$id)->update(['mobile'=>$mobile,'account'=>$mobile]);
            if ($data) {
                $this->error(__('修改手机号成功'));
            } else {
                $this->success('修改手机号失败');
            }
        }

    }

    /**
     * @ApiTitle    (收件箱)
     * @ApiSummary  (收件箱)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/inbox)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
        'id':1
        'type':类型
        'title':标题
        'sendtime':时间
        'status':1:拆封  0:未拆封
        'content':内容
         }

     * })
     */
    public function inbox()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $res = Db::name('inbox')->where('uid',$id)->paginate(10);
        $this->success('ok',$res);
    }



    /**
     * @ApiTitle    (投诉箱)
     * @ApiSummary  (投诉箱)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/complaint)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
        'id':1
        'gid':1
        'sn':投诉单号
        'title':投诉类型
        'sendtime':时间
        'status':0:投诉中  1:已解决
        'content':内容
     * })
     */
    public function complaint()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $res = Db::name('complaint')->where('uid',$id)->order('status desc')->paginate(10);
        $this->success('ok',$res);
    }


    /**
     * @ApiTitle    (投诉)
     * @ApiSummary  (投诉)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/onComplaint)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="gid", type="varchar", required=false, description="订单id")
     * @ApiParams   (name="title", type="varchar", required=false, description="投诉类型")
     * @ApiParams   (name="content", type="varchar", required=false, description="描述")
     * @ApiParams   (name="mobile", type="varchar", required=false, description="电话")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
        "code": 1,
        "msg": "投诉成功,请等待客服审核",
        "time": "1627553755",

     * })
     */
    public function  onComplaint()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
	    //添加分组，只有投诉咨询和一级客服可以分配
	    $auth_group = DB::name("auth_group")->where(['name'=>['in',['投诉咨询','一级客服']]])->column('id');
	    $auth_group_access = DB::name("auth_group_access")->where(['group_id'=>['in',$auth_group]])->column("uid");
	    $admin_id = Db::name('admin')->where(['status'=>'normal','id'=>['in',$auth_group_access]])->orderRaw('Rand()')->value('id');
        $gid = $this->request->post('gid');
        $title = $this->request->post('title');
        $content = $this->request->post('content');
        $mobile = $this->request->post('mobile');
        $rand = $id < 9999 ? mt_rand(100000, 99999999) : mt_rand(100, 99999);
        $com_sn = date('Yhis') . $rand;
        $ids = str_pad($id, (24 - strlen($com_sn)), '0', STR_PAD_BOTH);
        $sn = 'N'. $com_sn . $ids;
        $res = [
            'uid'   =>  $id,
            'gid'   =>  $gid,
            'sn'    =>  $sn,
            'title' =>  $title,
            'content'   =>  $content,
            'mobile'    =>  $mobile,
            'sendtime'  =>  time(),
            'status'    =>  1
        ];
        $data = Db::name('complaint')->insert($res);
        if ($data) {
            $easy = new Goeasy();
            $msg = '您有新的投诉订单了!';
            $easy->sendToAdmin($admin_id,$msg);
            Db::name('order')->where('id',$gid)->update(['complaint'=>1]);
            $this->success('投诉成功,请等待客服审核');
        } else {
            $this->error('投诉失败');
        }
    }



    /**
     * @ApiTitle    (投诉箱详情)
     * @ApiSummary  (投诉箱详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/complaintInfo)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="varchar", required=false, description="订单id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
        'id':1
        'gid':1
        'sn':投诉单号
        'title':投诉类型
        'sendtime':时间
        'status':(0:未回复 1:已回复)
        'content':描述
        "order": {
            "id":
            "image":
            "title": 商品名
            "type_name": 类型
            "game": 游戏
            "area": 区服
            "order_sn": 编号
            "amount": 成交价
            "complaint": (0:未投诉 1:已解决 2:处理中)
        }
     * })
     */
    public function  complaintInfo()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $oid = $this->request->post('id');
        $res = Db::name('complaint')->where('uid',$id)->where('gid',$oid)->find();
        $res['order'] = Db::name('order')->field('id,image,title,type_name,game,price,area,server,order_sn,amount,complaint')->where('id',$oid)->find();
        $res['order']['image'] = config('api_image_url') . $res['order']['image'];
        $this->success('ok',$res);
    }



    /**
     * @ApiTitle    (我的提问)
     * @ApiSummary  (我的提问)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/message)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="varchar", required=false, description="1:咨询 2:投诉 3:建议")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
    'id':1
    'status':0:未回复 1:已回复
    'type':1:咨询 2:投诉 3:建议
    'title':标题
    'createtime':1627553755

    }
     * })
     */
    public function message()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $type = $this->request->post('type');
        if (!empty($type)) {
            $res = Db::name('message')->field('id,status,type,title,createtime')->where('uid',$id)->where('type',$type)->paginate(10);
        } else {
            $res = Db::name('message')->field('id,status,type,title,createtime')->where('uid',$id)->paginate(10);
        }

        $this->success('ok',$res);
    }




    /**
     * @ApiTitle    (发送我的提问展示页)
     * @ApiSummary  (发送我的提问展示页)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/messageList)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
        "code": 1,
        "msg": "ok",
        "time": "1627553755",
        "data": {
            'id':1
            'name':标题名
            }
     * })
     */
    public function messageList()
    {
        $token = $this->request->header('token');
        $this->is_login($token);
        $res = Db::name('message_type')->field('id,name')->select();
        $this->success('ok',$res);
    }




    /**
     * @ApiTitle    (发送问题)
     * @ApiSummary  (发送问题)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/sendMessage)
     * @ApiHeaders	(name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="varchar", required=false, description="1:咨询 2:投诉 3:建议")
     * @ApiParams   (name="mobile", type="varchar", required=false, description="电话")
     * @ApiParams   (name="name", type="varchar", required=false, description="标题名")
     * @ApiParams   (name="desc", type="varchar", required=false, description="内容")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "发送成功,请耐心等待客服回复",
    "time": "1627553755",

     * })
     */
    public function sendMessage()
    {
        $token = $this->request->header('token');
        $id = Db::name('personal')->where('token',$token)->value('id');
        $this->is_login($token);
        $post = $this->request->post();
        $res = [
            'type'  =>  $post['type'],
            'contact_code'    =>  $post['mobile'],
            'title' =>  $post['name'],
            'desc'  =>  $post['desc'],
            'uid'   =>  $id,
            'createtime'    =>  time(),
            'status'    =>  0
        ];
        $data = Db::name('message')->insert($res);
        if ($data) {
            $this->success('发送成功,请耐心等待客服回复');
        } else {
            $this->error('发送失败');
        }
    }



    /**
     * @ApiTitle    (问题详情)
     * @ApiSummary  (问题详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (api/Personal/messageInfo)
     * @ApiParams   (name="id", type="varchar", required=false, description="问题id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    "code": 1,
    "msg": "ok",
    "time": "1627553755",
    "data": {
        'id':1
        'type':问题类型 1:咨询 2:投诉 3:建议
        'title':标题
        'desc':描述
        'contact_code':电话号码
        'status':0:未回复 1:已回复
        'createtime':添加时间
        'reply_content':回复内容
        'replytime':回复时间

    }
     * })
     */
    public function messageInfo()
    {
        $id = $this->request->post('id');
        $res = Db::name('message')->field('id,type,title,desc,contact_code,status,createtime,reply_content,replytime')->where('id',$id)->find();
        $this->success('ok',$res);
    }




    public function httpRequest($url, $data = '', $method = 'GET')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        if ($method == 'POST') {
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data != '') {
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
        }

        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }

}
